<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UserControllerSiku extends MX_Controller {

    private $_template = "siku_iainmataram";

    function __construct()
	{
		parent::__construct();
        $this->access_library_siku->is_logged_in();
		$this->load->model("user_model");
    }

    function checkLoginData()
    {
        $this->load->model("user_model");

        $postUsername = strip_quotes($this->input->post('username'));
        $postPassword = $this->security->xss_clean($this->input->post('password'));

        $dataLogin = $this->user_model->getDataLogin($postUsername);

        if($dataLogin->num_rows()>0){

          $dataLogin = $dataLogin->row();
          if (md5($dataLogin->PASSWORD)==md5($postPassword)) {

              $ua = $_SERVER['HTTP_USER_AGENT'];
              if (strpos($ua,'MSIE') != false && strpos($ua,'Opera') === false)
              {
               if (strpos($ua,'Windows NT 5.2') != false)
               {
                if(strpos($ua,'.NET CLR') === false) return;
               }
               if (substr($ua,strpos($ua,'MSIE')+5,1) < 7)
               {
                redirect('change-your-browser', 'location');
               }
              }

              $sessionData = array(
                            'siku_user_username' => $postUsername,
                            'siku_user_id' => $dataLogin->ID
                            //'siku_user_nama_prodi' => $dataLogin->NAMA_PRODI
                            /*'siku_system_semester' => $viewKontrolData->KODE_SEMESTER,
                            'siku_system_tahun_ajaran' => $viewKontrolData->KODE_TAHUN,
                            'siku_system_status' => $viewKontrolData->KODE_STATUS*/
                            );
               $this->session->set_userdata($sessionData);
               return TRUE;
           }
           else
               return FALSE;
         }
         else
           return FALSE;
    }

	function login()
	{
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('chk_pwd', '', 'callback_checkLoginData');
        $this->form_validation->set_message('required', 'Error: Masukkan %s');
        $this->form_validation->set_message('checkLoginData', 'Error: Username or Password is incorrect');

        if ($this->form_validation->run($this) == FALSE)
        {
          $this->load->view('login');
        }
        else
          redirect('siku', 'refresh');
	}

    function logout() {
        $data = array(
                 'siku_user_username' => 0,
                 'siku_user_id' => 0
                 //'siku_user_nama_prodi' => 0
                 /*'siku_system_semester' => 0,
                 'siku_system_tahun_ajaran' => 0,
                 'siku_system_status' => 0*/
				 );

        $this->session->sess_destroy();
        $this->session->unset_userdata($data);
		redirect('siku/login.html', 'refresh');
	}
    function viewWelcome()
	{
        $data['page'] = 'siku_home';
        $this->load->view($this->_template,$data);
	}

    function cek_pass_baru()
    {
        if($this->input->post('pass_baru') == $this->input->post('pass_baru_2'))
        {
          $dataPass = $this->user_model->getPassSiku($this->session->userdata('siku_user_id'),$this->input->post('pass_lama'));

          if($dataPass == 1  && $this->input->post('pass_baru')!=NULL && $this->input->post('pass_baru_2')!=NULL)
	      {
            $this->user_model->updatePassSiku($this->session->userdata('siku_user_id'),$this->input->post('pass_baru'));

            $status = 1;
		  }
          else if($dataPass == 1 && $this->input->post('pass_baru')== NULL)
          {
            $status = 4;
          }
          else if($dataPass == 1 && $this->input->post('pass_baru_2')== NULL)
          {
            $status = 5;
          }
          else
          {
            $status = 2;
          }
        }
        else
          $status = 3;

        return $status;
    }
	
    function editPassword()
	{
	    $status = $this->cek_pass_baru();

        if($this->input->post('key') != 1)
          $status = 0;

        switch ($status)
        {
          case 1:

             $data['message'] = 'Data telah disimpan. <br />Simpan data akun Anda dengan baik.';
             $data['clr'] = 'p_messages_success';
             break;

          case 2:

             $data['message'] = 'Password lama salah.';
             $data['clr'] = 'p_messages_error';
             break;

          case 3:

             $data['message'] = 'Ulangi Re-password baru';
             $data['clr'] = 'p_messages_error';
             break;

          case 4:

             $data['message'] = 'Password baru tidak boleh kosong.';
             $data['clr'] = 'p_messages_error';
             break;

          case 5:

             $data['message'] = 'Field Re-password baru tidak boleh kosong.';
             $data['clr'] = 'p_messages_error';
             break;

          default:

             $data['message'] = NULL;
             break;
        }

        $data['page'] = 'ganti_password';
        $this->load->view($this->_template,$data);
	}
}

